package jar

//单例类私有构造
class AssociatedObject private(field:String) {

  println("create "+this)

  override def toString: String = AssociatedObject.getClass.getName+"[\"field\":"+field+"]"

}

//所创建的单例对象
object AssociatedObject{

  //私有属性,map类型
  private val fieldMap:Map[String,AssociatedObject]=Map(
    "key1"->new AssociatedObject("key1"),
    "key2"->new AssociatedObject("key2"),
    "key3"->new AssociatedObject("key3")
  )


  def apply(key: String) = {
      println("对象 apply 方法初始化对象")
      if(fieldMap.contains(key)) fieldMap(key) else "no such key"
  }

  def getValue(key:String)={
    if(fieldMap.contains(key)) fieldMap(key) else "no such key"
  }


  def main(args: Array[String]): Unit = {

    println(AssociatedObject("key1"))

    println(AssociatedObject getValue "key1")

  }

}


object Foo{

  var x:Int=10;

  def apply()={
    println("hello,world~")


  }

  private var num:Int=10



  def main(args: Array[String]): Unit = {
    Foo()

    println(Foo.x)

  }

}